﻿using UnityEngine;
using System.Collections;

/// <summary>  
/// 鼠标控制器  
/// </summary>  
public class MouseController : PlatformController
{
    /// 鼠标枚举  
    enum MouseTypeEnum
    {
        LEFT = 0
    }

    /// <summary>  
    /// 缩放距离  
    /// </summary>  
    private float scrollDistance;
    
    /// <summary>  
    /// 鼠标按住状态  
    /// </summary>  
    private bool mousePressStatus;


    public int maxHoldCount = 3;
    private Vector2 lastViewPos;
    private bool scaled = false;

    void Update()
    {
        // 按下鼠标、轴  
        if (Input.GetMouseButtonDown((int)MouseTypeEnum.LEFT))
        {
            this.mousePressStatus = true;
            // 触发开始回调函数  
            if (this.beginCallback != null) this.beginCallback(GetViewPortPos(Input.mousePosition));
        }
        // 松开鼠标、轴  
        if (Input.GetMouseButtonUp((int)MouseTypeEnum.LEFT))
        {
            this.mousePressStatus = false;
            // 触发结束回调函数  
            Vector2 curViewPos = GetViewPortPos(Input.mousePosition);
            Vector2 diff = curViewPos - lastViewPos;
            if (this.endCallback != null) this.endCallback(diff);
        }
        // 如果鼠标在按住状态  
        if (this.mousePressStatus)
        {
            lastViewPos = GetViewPortPos(Input.mousePosition);
            // 触发移动回调函数  
            if (this.moveCallback != null) this.moveCallback(GetViewPortPos(Input.mousePosition));
        }
        // 鼠标滚轮拉近拉远  
        this.scrollDistance = Input.GetAxis("Mouse ScrollWheel");
        // 触发缩放回调函数  
        if (this.scrollDistance != 0f)
        {
            if (this.scaleCallback != null)
            {
                this.scaleCallback(-this.scrollDistance);
                scaled = true;
            }
        }

        if (this.scrollDistance == 0f && scaled == true)
        {
            if (this.endScaleCallback != null)
            {
                this.endScaleCallback(Time.time);
                scaled = false;
            }
        }

        // 触发每帧执行更新  
        if (this.updateCallback != null) this.updateCallback();
    }
}